#include "fxcc/core/graph/common/EffectsLoader.h"

std::unordered_map<std::string, std::shared_ptr<fxcc::graph::common::EffectsDesc>> EffectsLoader::m_EffectsDescs;

bool EffectsLoader::LoadFile(const fxcc::File& file)
{
    switch (file.m_FileType)
    {
    case fxcc::FileType::_Effects_:
        return LoadPath(file.m_RelativePath).get();
    default:
        return false;
    }
}

std::shared_ptr<fxcc::graph::common::EffectsDesc> EffectsLoader::GetEffectDesc(const std::string& key)
{
    auto it = m_EffectsDescs.find(key);
    if (it != m_EffectsDescs.end())
    {
        return it->second;
    }
    return std::shared_ptr<fxcc::graph::common::EffectsDesc>();
}

std::shared_ptr<fxcc::graph::common::EffectsDesc> EffectsLoader::LoadPath(const std::string& path, bool force_reload)
{
    auto it = m_EffectsDescs.find(path);

    if (force_reload)
    {
        m_EffectsDescs.erase(it);
    }
    else if (it != m_EffectsDescs.end())
    {
        return it->second;
    }

    auto inst = std::make_shared<fxcc::graph::common::EffectsDesc>();
    fxcc::yml::Serializer ymlser;
    bool flag = ymlser.DecodePath<fxcc::graph::common::EffectsDesc>(path, *inst);
    m_EffectsDescs[path] = inst;
    return inst;
}
